var icd;
var ccd;
var statusArray = {};
//var source_nav = [];
var enToCh ={
    "attribute": "属性",
    "name": "名称",
    "type": "类型",
    "multiplicity":"多重性",
    "visibility":"可见性",
    "default":"默认值",
    "propertyStrings":"属性性质",
    "constraint":"约束",
    'generalization':'泛化关系',
    'association':'关联关系',
    'composition':'组成关系',
    'aggregation':'聚合关系'
}
var chToEn ={
    "属性":   "attribute",
    "名称":   "name",
    "类型":   "type",
    "多重性": "multiplicity",
    "可见性": "visibility",
    "默认值": "default",
    "属性性质":"propertyStrings",
    "约束"   :"constraint",
    '泛化关系':'generalization',
    '关联关系':'association',
    '组成关系':'composition',
    '聚合关系':'aggregation'
}
var regex = /^_/;
var m_attributeElem = ['type','multiplicity','visibility','default','propertyStrings','constraint'];

icd_nav_classdiagram_show = function(doc){
    icd = doc;
    statusArray["_id"] = doc["_id"];
    statusArray["ccd_id"] = doc["ccd_id"];
    //source_nav = [];

    var that = $(".nav.nav-list.bs-docs-sidenav");
    var header_cd = $(that).children(".nav-header-class");
    var header_class = $(that).children(".nav-header-relation");
    var header_relation = $(that);

    for(var a in doc.cd){
        var html = '<li name='+a+ ' class="cd"><a href="javascript:;"><i class="icon-chevron-right"></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ a +'</a></li>';
        $(header_cd).before(html);
        statusArray["cd"] = a;
    };
    for(var b in doc.class){
        for(var b_val in doc.class[b]._value){
            var html =  '<li name='+b+' class="class"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ b_val +'</a></li>';
            $(header_class).before(html);
        }
    };
    for(var c in doc.relation){
        for(var c_val in doc.relation[c]._value){
            var html = '<li name='+c+' class="relation"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ c_val +'</a></li>';
            $(header_relation).append(html);
        }
    };
    getSourceNav();
    $(header_cd).prev().trigger("click");
}

getSourceNav = function(){
    source_nav = [];
    for(var a in icd.cd){
        source_nav.push(a);
    };
    for(var b in icd.class){
        source_nav.push(b);
    };
    for(var c in icd.relation){
        source_nav.push(c);
    };
}

icd_content_show = function(type,item){
    switch(type[0]){
        case 'cd': icd_content_cd_show(item);
            break;
        case 'class':icd_content_class_show(item);
            break;
        case 'relation': icd_content_relation_show(item);
            break;
    };
}

icd_content_cd_show = function(item){
    statusArray['cd'] = item.key;

    var root = $(".span6-compact.content-detail");
    var list_name = $(root).find(".recommend-element.type_cdName");

    /*
    for(var name in icd['cd']){
        //alert(name);
        $(list_name).children(".span4").children("p").text(item);
        $(list_name).parent().children(".control-sub").find("input").val(item);
    }
    */
    $(list_name).parent().children("input[type='hidden']").val(item.key);
    $(list_name).children(".span4").children("p").text(item.value);
    $(list_name).parent().children(".control-sub").find("input[type='text']").val(item.value);


    var list_description = $(root).find(".recommend-element.type_cdDescription");
    $(list_description).children("p").text(icd['cd'][item.key]['_description']);
    $(list_description).children(".controls-small").children("textarea").text(icd['cd'][item.key]['_description']);
}

icd_content_class_show = function(item){
    statusArray['class'] = item.key;
    $("#icd-class-name").find(".recommend-element p").text(item.value);
    $("#icd-class-name").find(".control-sub input[type='text']").val(item.value);
    var list = $("#icd-class-name").parent().children(".accordion");
    for(var name in icd['class'][item.key]){
        //if(name != '_nor' && name!= '_value'){
        if(regex.test(name) === false){
            $.ajax({
                url: '/post/html/icd-element-attribute',
                type: 'post',
                data: {},
                async: false,   //这样很蛋疼饿
                success: function(html){
                    $(list).append(html);
                    icd_element_attribute_show($(list).children().last(),name,icd['class'][item.key][name]);
                }
            });
        }
    }
}

icd_element_attribute_show = function(handle,item,dir){
    $(handle).children("input[type='hidden']").val(item);
    var title= $(handle).find(".accordion-inner").children().eq(0);
    for(var value in dir["_value"]){
        $(title).find(".recommend-element p").text(value);
        $(title).parent().find(".control-sub input[type='text']").val(value);
        break;
    }

    var list = $(title).parent();

    var summary= {};
    summary["name"] = item;
    for(var name in dir){
        //if(name != '_nor'){
        if(regex.test(name) === false){
            $.ajax({
                url: '/post/html/icd-element',
                type: 'post',
                data: {name:name},
                async: false,   //这样很蛋疼饿
                success: function(html){
                    $(list).append(html);
                    icd_element_attributeElem_show($(list).children().last(),name,dir[name]);
                }
            });
        }
    }
    summaryIntoString($(list).parent().parent());
}

icd_element_attributeElem_show = function(handle,item,dir){
    var itemCh = enToCh[item];
    $(handle).find("label").text(itemCh);


    for(var name in dir){
        //if(name != '_nor'){
        if(regex.test(name) === false){
            $(handle).find(".recommend-element p").text(name);
            $(handle).find(".control-sub input[type='text']").val(name);
            return name;//因为这里只有一个值
        }
    }
}

summaryIntoString = function(that){
    //function(summary,list){
    var inner = $(that).find(".accordion-inner");
    var label = $(inner).find(".row label");
    var p=$(inner).find(".row p");

    var summary={};
    for(var i= 0;i<label.length;i++){
        summary[$(label[i]).text()] = $(p[i]).text();
    }

    var text = "";
    switch(summary['可见性']){
        case 'private': text +='- ';  break;
        case 'public': text +='+ ';  break;
        case 'protected': text +='# ';  break;
        case 'package': text +='~ '; break;
    };

    text += summary['名称']+" : "
    if(summary['类型']!=undefined) text+=summary['类型'];
    if(summary['多重性']!=undefined) text+='[ '+summary['多重性']+' ]';

    $(that).find(".accordion-heading p").text(text);
}

icd_content_relation_show = function(item){
    statusArray['relation'] = item.key;
    //name
    $("#icd-relation-name").find(".recommend-element p").text(item.value);
    var input = $("#icd-relation-name").find(".control-sub input[type='text']");
    var className = item.value;

    className = className.split("-");
    $(input[0]).val(className[0]);
    $(input[1]).val(className[1]);
    //type
    var typeName,typeNumber;
    for(var name in icd['relation'][item.key]){
        //if(name!='_nor' && name !='_value'){
        if(regex.test(name) === false){
            switch(name){
                case 'generalization':typeName='泛化关系';typeNumber=1;break;
                case 'association':typeName='关联关系';typeNumber=2;break;
                case 'composition':typeName='组成关系';typeNumber=3;break;
                case 'aggregation':typeName='聚合关系';typeNumber=4;break;
            }
            var mark = 1;
            break;
        }
    };
    if(mark != 1)   {typeName='';typeNumber=null;return;}
    var type = $("#icd-relation-name").parent().find(".recommend-element.type_relationType");
    $(type).children(".span4").children("p").text(typeName);
    $(type).parent().children(".control-sub").children().eq(typeNumber).children("input[type='radio']").attr('checked','checked');
    //TypeElem
    $.ajax({
        url: '/post/html/icd-element-relation-sub-'+name,
        type: 'post',
        data: {},
        async: false,   //这样很蛋疼饿
        success: function(html){
            $("#icd-relation-detail").children().remove();
            $("#icd-relation-detail").append(html);
            icd_element_relationType_show[name]($("#icd-relation-detail"),className,icd['relation'][item.key][name]);
        }
    });
}

icd_element_relationType_show ={ //function(handle,className,type,dir){
    generalization: function(handle,className,dir){
        //alert("generalization");
        statusArray['relationType'] = "generalization";
        var ClassNameShow = $(handle).find(".control-group p");
        var ClassNameInput = $(handle).find(".control-group input[type='text']");
        $(ClassNameShow[0]).text(className[0]);
        $(ClassNameShow[1]).text(className[1]);
        $(ClassNameInput[0]).val(className[0]);
        $(ClassNameInput[1]).val(className[1]);
    },
    association : function(handle,className,dir){
        statusArray['relationType'] = "association";
        association_init(handle,className,dir);
    },
    aggregation : function(handle,className,dir){
        statusArray['relationType'] = "aggregation";
        association_init(handle,className,dir);
    },
    composition : function(handle,className,dir){
        statusArray['relationType'] = "composition";
        association_init(handle,className,dir);
    }
};

association_init = function(handle,className,dir){
    var Show = $(handle).find(".control-group p");
    var Input = $(handle).find(".control-group input[type='text']");
    $(Show[0]).text(className[0]);
    $(Show[2]).text(className[1]);
    $(Input[0]).val(className[0]);
    $(Input[2]).val(className[1]);
    for(var count in dir['multiply1']){
        //if(count != '_nor'){
        if(regex.test(count) === false){
            $(Show[1]).text(count);
            $(Input[1]).val(count);
        }
    }
    for(var count in dir['multiply2']){
        //if(count != '_nor'){
        if(regex.test(count) === false){
            $(Show[3]).text(count);
            $(Input[3]).val(count);
        }
    }
};


clickOnNavItem = function(item){
    $(item).parent().children().removeClass("active");
    //$(m_old_active).removeClass("active");
    //m_old_active = item;
    $(item).addClass("active");
    var value = {
        key:$(item).attr('name'),
        value:$(item).text()
    };
    var type;
    if($(item).hasClass("cd")){
        type='cd';
        url = '/post/html/icd-content-classdiagram';
    }
    else if($(item).hasClass("class")){
        type='class'
        url = '/post/html/icd-content-class';
    }
    else if($(item).hasClass("relation")){
        type = 'relation';
        url = '/post/html/icd-content-relation';
    }
    else return;

    $.ajax({
        url: url,
        type: 'post',
        success: function(html){
            $(".content-detail").children().remove();
            $(".content-detail").append(html);
            icd_content_show([type],value);
        }
    });
};


//search
getSource = function(icd_dir,ccd_dir){
    //可以加一个原始项orig
    var array=[];
    var msg=[];
    for(var key in ccd_dir){
        //if(key != '_nor' && key != '_description'){
        if(regex.test(key) === false){
            //alert("key:"+key);
            if(icd_dir === null){
                array.push([key,ccd_dir[key]['_nor']]);
            }
            else if(icd_dir[key] === undefined){
                array.push([key,ccd_dir[key]['_nor']]);
            }
        }
    };
    array.sort(function(a,b){a[1]-b[1];});
    for(var i=0;i<array.length;i++){
        msg.push(array[i][0]);
    }
    //alert("msg:"+msg);
    return msg;
}

//Legal
classIsLegal = function(){

}

relationIsLegal = function(){

}
